package com.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.dao.CommunityDao;
import com.dao.impl.CommunityDaoImpl;
import com.entity.household_location;

@WebServlet("/AddHomeServlet")
public class AddHomeServlet extends HttpServlet{
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String ID = request.getParameter("ID"); //获取jsp页面传过来的参数
        String Name = request.getParameter("Name");
        String Sex = request.getParameter("Sex");
        String HomephoneNo = request.getParameter("HomephoneNo");
        String TelephoneNo = request.getParameter("TelephoneNo");
        String WorkUnit = request.getParameter("WorkUnit");
        String WorkPhone = request.getParameter("WorkPhone");
        String WorkAddress = request.getParameter("WorkAddress");
        String CheckinTime = request.getParameter("CheckinTime");
        String CommunityID = request.getParameter("CommunityID");
        String BuildingID = request.getParameter("BuildingID");
        String ApartmentID = request.getParameter("ApartmentID");
        String RoomID = request.getParameter("RoomID");

        household_location location = new household_location(); //实例化一个对象，组装属性
        location.setID(ID);
        location.setName(Name);
        location.setSex(Sex);
        location.setHomephoneNo(HomephoneNo);
        location.setTelephoneNo(TelephoneNo);
        location.setWorkUnit(WorkUnit);
        location.setWorkPhone(WorkPhone);
        location.setWorkAddress(WorkAddress);
        location.setCheckinTime(CheckinTime);
        location.setCommunityID(CommunityID);
        location.setBuildingID(BuildingID);
        location.setApartmentID(ApartmentID);
        location.setRoomID(RoomID);

        CommunityDao communityDao = new CommunityDaoImpl();

        if(communityDao.add(location)){
            //request.setAttribute("ID", ID);  //向request域中放置参数

            request.getRequestDispatcher("/index.jsp").forward(request, response);  //转发到登录页面
        }else{
            response.sendRedirect("addhome.jsp");//重定向到首页

        }
    }
}
